﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.SqlClient;

namespace WanWebApplication
{
    public partial class Rg : System.Web.UI.Page
    {

        public string OpenId
        {
            get
            {
                return Request.QueryString["openid"];
            }
        }
        public bool IsCache
        {
            get
            {
                if (Request.QueryString["isCache"] == "1")
                {
                    return true;
                }
                return false;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string idStr = Request.QueryString["Id"];
                if (!string.IsNullOrEmpty(idStr))
                {
                    try
                    {
                        UiRefresh();
                    }
                    catch (ArgumentNullException ex) //通常wechat的强制断开连接是AgreumentNullExcption
                    {
                        if (!IsCache)
                        {
                            Response.Redirect("./Wechat/ErrorInfo.aspx?info=" + HttpUtility.UrlEncode("授权服务器连接中断！"));
                        }
                    }
                }
            }
        }
        private void UiRefresh()
        {
            txtOperateSummary.Text = "";
            #region  数据绑定
            string idStr = Request.QueryString["Id"];
            int temp = 0;
            //if (!int.TryParse(idStr, out temp))
            //{
            //    Response.Clear();
            //    Response.Redirect("./Wechat/ErrorInfo.aspx?info=非法访问！");
            //    Response.End();
            //    return;
            //}
            if (string.IsNullOrWhiteSpace(OpenId))
            {
                Response.Redirect("Wechat/Login.aspx?id=" + HttpUtility.UrlEncode(idStr));
                return;
            }
            //var br = bll.ValidateUserAuth(OpenId, int.Parse(idStr));
            //if (br.ResultState != RgUntility.Common.BizState.OK)
            //{
            //    Response.Clear();
            //    Response.Redirect("./Wechat/ErrorInfo.aspx?info=" + br.Message);
            //    Response.End();
            //    return;
            //}
            //var goods = bll.GetGoodsView(int.Parse(idStr));


            //lbAgreementCode.Text = Server.HtmlEncode(goods.AgreementCode);
            //lbDeviceModel.Text = Server.HtmlEncode(goods.DeviceModel);
            //lbItemType.Text = Server.HtmlEncode(goods.ItemTypeStr);
            //lbPrice.Text = Server.HtmlEncode(goods.Price.Value.ToString("C2"));
            ////
            //lbSupplier.Text = HttpUtility.HtmlEncode(goods.SupplierName);
            //lbSupplierContactPersonName.Text = HttpUtility.HtmlEncode(goods.SupplierContactPersonName);
            //lbSupplierContactPeronTel.Text = HttpUtility.HtmlEncode(goods.SupplierContactPersonTel);
            ////
            //lbProjectName.Text = Server.HtmlEncode(goods.ProjectFullPath);
            //lbRecevieDepatName.Text = Server.HtmlEncode(goods.ReceviceUserName);
            //lbRecevieState.Text = HttpUtility.HtmlEncode(goods.ReceiveStateEnumValStr);
            //lbSN.Text = Server.HtmlEncode(goods.SN);
            //lbSpecificaitons.Text = Server.HtmlEncode(goods.Specifications);
            //lbUse.Text = Server.HtmlEncode(goods.UseEx);
            //lbEt.Text = HttpUtility.HtmlEncode(goods.ElectrifyStateEnumValStr);
            //txtSummary.Text = goods.ElectrifySummary;
            ////
            //var brRlt = bll.IsRlt(OpenId, int.Parse(idStr));//判断是否为关联用户
            //if (brRlt.ResultState != RgUntility.Common.BizState.OK)
            //{
            //    divError.Style["display"] = "block";
            //    txtOperateSummary.Text = brRlt.Message;
            //    //
            //    btnRecived.CssClass = "button button-disabled";
            //    btnRecived.Enabled = false;
            //    aEt.Attributes["class"] = "button button-disabled";
            //}
            //else
            //{
            //    var brIsEt = bll.IsAllowTest(int.Parse(idStr), OpenId);
            //    if (brIsEt.ResultState == RgUntility.Common.BizState.OK)
            //    {
            //        aEt.HRef = "~/Wechat/ElectrifyTest.aspx?Id=" + idStr + "&openid=" + OpenId;
            //    }
            //    else
            //    {
            //        aEt.Attributes["class"] = "button button-disabled";
            //    }
            //    var brIsReceived = bll.IsAllowReceived(int.Parse(idStr), OpenId);
            //    if (brIsReceived.ResultState != RgUntility.Common.BizState.OK)
            //    {
            //        btnRecived.CssClass = "button button-disabled";
            //        btnRecived.Enabled = false;
            //    }
            //    if (brIsReceived.ResultState != RgUntility.Common.BizState.OK)
            //    {
            //        divError.Style["display"] = "block";
            //        txtOperateSummary.Text = HttpUtility.HtmlEncode(txtOperateSummary.Text + brIsReceived.Message);
            //    }
            //    if (brIsEt.ResultState != RgUntility.Common.BizState.OK)
            //    {
            //        divError.Style["display"] = "block";
            //        if (!string.IsNullOrWhiteSpace(txtOperateSummary.Text))
            //        {
            //            txtOperateSummary.Text = HttpUtility.HtmlEncode(txtOperateSummary.Text) + "<br/>" + HttpUtility.HtmlEncode(brIsEt.Message);
            //        }
            //        else
            //        {
            //            txtOperateSummary.Text = HttpUtility.HtmlEncode(txtOperateSummary.Text + brIsEt.Message);
            //        }
            //    }
           // }
            #endregion
        }

        protected void btnRecived_Click(object sender, EventArgs e)
        {
            try
            {
                //var idStr = Request.QueryString["Id"];
                //if (string.IsNullOrWhiteSpace(OpenId))
                //{  //曾经发现过有些微信浏览器掉session
                //    var log = new WanLog();
                //    log.Content = "尝试收货但是授权丢失！";
                //    log.OperateUserType = WanBizModule.Wechat.LogSearch.LogBll.AuthWechatWeb;
                //    LogBll.Write(log);
                //}
                //var br = bll.QRRecevieGoods(int.Parse(idStr), OpenId, this);

                //btnRecived.CssClass = "button button-disabled";
                //btnRecived.Enabled = false;
                //WebHelperForClient.Alert(this, br.Message);
                //UiRefresh();
            }
            catch (ArgumentNullException ex) //通常wechat的强制断开连接是AgreumentNullExcption
            {
                if (!IsCache)
                {
                    Response.Redirect("./Wechat/ErrorInfo.aspx?info=" + HttpUtility.UrlEncode("授权服务器连接中断！"));
                }
            }
        }

    }
}